package com.estronger.shopmanage.ui.order.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.adapter.order.StatisticalAdpter;
import com.estronger.shopmanage.base.BaseActivity;
import com.estronger.shopmanage.base.MyApplication;
import com.estronger.shopmanage.bean.ReturnMsgEntity;
import com.estronger.shopmanage.bean.ldfxEntity;
import com.estronger.shopmanage.utils.JsonUtil;
import com.estronger.shopmanage.utils.TimeUtils;
import com.estronger.shopmanage.utils.Toast;
import com.estronger.shopmanage.view.CircularProgress;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONArray;
import org.json.JSONObject;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import cn.jesse.nativelogger.NLogger;

public class StatisticalDetailActivity extends BaseActivity implements
        View.OnClickListener {

    Context mContext;
    private View rootView;//根目录View
    private TextView tvfTitle;
    RecyclerView datalist;
    //    StatisticalEntity data;
//    List<StatisticalEntity> mlist;
    StatisticalAdpter adpter;

    boolean isShowBar = false;
    //bar
    LinearLayout toolbar;
    TextView tvtoday;
    TextView tvyesterday;
    TextView tvcurweek;
    TextView tvcurmonth;
    TextView tvcustom;
    CircularProgress loding_view;
    ImageView dwarr;
    TextView nodata;

    //bar2
    RelativeLayout rl41;
    LinearLayout downlist;
    RelativeLayout downlistoutside;
    TextView tvdownlist;

    //tipbar
    RelativeLayout tipbar;
    TextView tvtip;

    //获取类型
    String type = "0";
    //楼盘id
    String buildid = "0";
    //
    List<ldfxEntity> mmlist;
    //开始结束时间
    String start;
    String end;

    @Override
    protected void init() {
    }


    @Override
    protected void initView() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mContext = StatisticalDetailActivity.this;
        rootView = View.inflate(mContext, R.layout.layout_activity_statistical, null);
        setContentView(rootView);
        tvfTitle = (TextView) findViewById(R.id.ab_title);

        tvfTitle.setOnClickListener(null);
        // findid
        toolbar = (LinearLayout) rootView.findViewById(R.id.toolbar);
        downlist = (LinearLayout) rootView.findViewById(R.id.downlist);
        downlist.setOnClickListener(this);
        downlistoutside = (RelativeLayout) rootView.findViewById(R.id.downlistoutside);
        downlistoutside.setOnClickListener(this);
        rl41 = (RelativeLayout) rootView.findViewById(R.id.rl41);
        tvdownlist = (TextView) rootView.findViewById(R.id.tvdownlist);
        datalist = (RecyclerView) rootView.findViewById(R.id.datalist);
        tvtoday = (TextView) rootView.findViewById(R.id.tvtoday);
        tvtoday.setTextColor(Color.parseColor("#ca3c33"));
        tvtoday.setOnClickListener(this);
        tvyesterday = (TextView) rootView.findViewById(R.id.tvyesterday);
        tvyesterday.setOnClickListener(this);
        tvcurweek = (TextView) rootView.findViewById(R.id.tvcurweek);
        tvcurweek.setOnClickListener(this);
        tvcurmonth = (TextView) rootView.findViewById(R.id.tvcurmonth);
        tvcurmonth.setOnClickListener(this);
        tvcustom = (TextView) rootView.findViewById(R.id.tvcustom);
        tvcustom.setOnClickListener(this);
        tipbar = (RelativeLayout) rootView.findViewById(R.id.tipbar);
        tvtip = (TextView) rootView.findViewById(R.id.tvtip);
        loding_view = (CircularProgress) rootView.findViewById(R.id.loding_view);
        dwarr = (ImageView) rootView.findViewById(R.id.imageView4);
        dwarr.setVisibility(View.GONE);
        nodata = (TextView) rootView.findViewById(R.id.nodata);

        // 设置布局管理器(RecyclerView需要)
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        datalist.setLayoutManager(linearLayoutManager);

        //初始化buildid
        buildid = getIntent().getStringExtra("buildid");
        String titlestr = getIntent().getStringExtra("title");
        tvfTitle.setText(titlestr.length() > 15 ? titlestr.substring(0, 15) + "..." : titlestr + "");
        int tabNum = getIntent().getIntExtra("tabNum", 1);
        returnDateSpace(tabNum);
        //加载数据
        start = returnDateSpace(tabNum).split(";;")[0];
        end = returnDateSpace(tabNum).split(";;")[1];
        loaddata();
    }

    @Override
    protected void initAction() {

    }

    private void loaddata() {
        try {
            String url = MyApplication.getApplication().getSERVER_URL() + "/web/common/API/apphandler.ashx";
            RequestParams params = new RequestParams();
            params.addBodyParameter("sf_action", "getpreordreport");
            params.addBodyParameter("usercode", MyApplication.getUserCode());
            params.addBodyParameter("gettype", type);
            params.addBodyParameter("buildid", buildid);
            params.addBodyParameter("startdate", start);
            params.addBodyParameter("enddate", end);

            HttpUtils httpUtils = new HttpUtils();
            httpUtils.configTimeout(15 * 1000).configSoTimeout(15 * 1000).configRequestRetryCount(3);
            httpUtils.configCurrentHttpCacheExpiry(0);
            httpUtils.send(HttpRequest.HttpMethod.POST, url, params,
                    new RequestCallBack<String>() {
                        @Override
                        public void onStart() {
                            super.onStart();
                            loding_view.setVisibility(View.VISIBLE);
                        }

                        @Override
                        public void onSuccess(ResponseInfo<String> responseInfo) {
                            try {
                                ReturnMsgEntity re = JsonUtil.returnMsgEntity(responseInfo.result);
                                if (re != null) {
                                    if (re.getSuccess()) {
                                        /**回调数据到前台**/
                                        mmlist = new ArrayList<ldfxEntity>();
                                        JSONArray jr = new JSONArray(re.getDataJson());
                                        for (int i = 0; i < jr.length(); i++) {
                                            JSONObject o = new JSONObject(jr.get(i).toString());
                                            ldfxEntity le = new ldfxEntity();
                                            le.setSno(o.getString("sno") + "");
                                            le.setName(o.getString("name") + "");
                                            le.setId(o.getString("buildid") + "");
                                            le.setNum(o.getString("num") + "");
                                            le.setColor(o.getString("color") + "");
                                            mmlist.add(le);
                                        }

                                        if (mmlist.size() > 0) {
                                            adpter = new StatisticalAdpter(mContext, mmlist);
                                            adpter.setOnItemClickListener(null);
                                            datalist.setAdapter(adpter);
                                            nodata.setVisibility(View.GONE);
                                        }

                                    } else {
                                        Toast.show(mContext, re.getMsg(), 2000);
                                        nodata.setVisibility(View.VISIBLE);
                                        adpter = new StatisticalAdpter(mContext, new ArrayList<ldfxEntity>());
                                        datalist.setAdapter(adpter);
                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                                nodata.setVisibility(View.VISIBLE);
                                adpter = new StatisticalAdpter(mContext, new ArrayList<ldfxEntity>());
                                datalist.setAdapter(adpter);
                            }
                            loding_view.setVisibility(View.GONE);
                        }
                        @Override
                        public void onFailure(HttpException e, String s) {
                            loding_view.setVisibility(View.GONE);
                            nodata.setVisibility(View.VISIBLE);
                            adpter = new StatisticalAdpter(mContext, new ArrayList<ldfxEntity>());
                            datalist.setAdapter(adpter);
                            if (s != null) {
                                NLogger.e("error:" + s);
                            }
                        }
                    });
        } catch (Exception e) {
            NLogger.e("加载统计数据异常:" + e.getMessage());
        }
    }

    private String formatterstr = "yyyy-MM-dd";

    private String returnDateSpace(int casetype) {
        String backdate = "";
        switch (casetype) {
            case 1:
                tvtoday.setTextColor(Color.parseColor("#ca3c33"));
                tvyesterday.setTextColor(Color.parseColor("#a2a2a2"));
                tvcurweek.setTextColor(Color.parseColor("#a2a2a2"));
                tvcurmonth.setTextColor(Color.parseColor("#a2a2a2"));
                tvcustom.setTextColor(Color.parseColor("#a2a2a2"));

                tipbar.setVisibility(View.GONE);
                tvtip.setText("");
                return TimeUtils.getSystime(formatterstr) + ";;" + TimeUtils.getSystime(formatterstr);
            case 2:
                tvtoday.setTextColor(Color.parseColor("#a2a2a2"));
                tvyesterday.setTextColor(Color.parseColor("#ca3c33"));
                tvcurweek.setTextColor(Color.parseColor("#a2a2a2"));
                tvcurmonth.setTextColor(Color.parseColor("#a2a2a2"));
                tvcustom.setTextColor(Color.parseColor("#a2a2a2"));

                tipbar.setVisibility(View.GONE);
                tvtip.setText("");

                return TimeUtils.getSystemPreDate(-1, formatterstr) + ";;" + TimeUtils.getSystemPreDate(-1, formatterstr);
            case 3:
                tvtoday.setTextColor(Color.parseColor("#a2a2a2"));
                tvyesterday.setTextColor(Color.parseColor("#a2a2a2"));
                tvcurweek.setTextColor(Color.parseColor("#ca3c33"));
                tvcurmonth.setTextColor(Color.parseColor("#a2a2a2"));
                tvcustom.setTextColor(Color.parseColor("#a2a2a2"));

                tipbar.setVisibility(View.GONE);
                tvtip.setText("");

                //先算今天是第几天 第一天从星期天开始
                int d = TimeUtils.getWeekOfDate(new Date(System.currentTimeMillis()));
                return TimeUtils.getSystemPreDate(-d, formatterstr) + ";;" + TimeUtils.getSystime(formatterstr);
            case 4:
                tvtoday.setTextColor(Color.parseColor("#a2a2a2"));
                tvyesterday.setTextColor(Color.parseColor("#a2a2a2"));
                tvcurweek.setTextColor(Color.parseColor("#a2a2a2"));
                tvcurmonth.setTextColor(Color.parseColor("#ca3c33"));
                tvcustom.setTextColor(Color.parseColor("#a2a2a2"));

                tipbar.setVisibility(View.GONE);
                tvtip.setText("");

                return TimeUtils.getSystime("yyyy-MM") + "-01" + ";;" + TimeUtils.getSystime(formatterstr);
        }
        return "";
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ab_title:
                if (isShowBar) {
                    rl41.setVisibility(View.GONE);
                    toolbar.setVisibility(View.VISIBLE);
                    isShowBar = false;
                } else {
                    rl41.setVisibility(View.VISIBLE);
                    toolbar.setVisibility(View.GONE);
                    isShowBar = true;
                }
                break;
            case R.id.tvtoday:
                start = returnDateSpace(1).split(";;")[0];
                end = returnDateSpace(1).split(";;")[1];
                loaddata();
                break;
            case R.id.tvyesterday:
                start = returnDateSpace(2).split(";;")[0];
                end = returnDateSpace(2).split(";;")[1];
                loaddata();
                break;
            case R.id.tvcurweek:

                start = returnDateSpace(3).split(";;")[0];
                end = returnDateSpace(3).split(";;")[1];
                loaddata();
                break;
            case R.id.tvcurmonth:

                start = returnDateSpace(4).split(";;")[0];
                end = returnDateSpace(4).split(";;")[1];
                loaddata();
                break;
            case R.id.tvcustom:
                tvtoday.setTextColor(Color.parseColor("#a2a2a2"));
                tvyesterday.setTextColor(Color.parseColor("#a2a2a2"));
                tvcurweek.setTextColor(Color.parseColor("#a2a2a2"));
                tvcurmonth.setTextColor(Color.parseColor("#a2a2a2"));
                tvcustom.setTextColor(Color.parseColor("#ca3c33"));

                startActivityForResult(new Intent(StatisticalDetailActivity.this, StatisticalSelectTimeActivity.class), 9999);

                break;
            case R.id.downlist:
                String titles = tvfTitle.getText().toString();
                String downs = tvdownlist.getText().toString();
                tvfTitle.setText(downs);
                tvdownlist.setText(titles);
                rl41.setVisibility(View.GONE);
                toolbar.setVisibility(View.VISIBLE);
                isShowBar = false;
                if (type.equals("0")) {
                    type = "1";
                } else if (type.equals("1")) {
                    type = "0";
                }
                loaddata();
                break;
            case R.id.downlistoutside:
                rl41.setVisibility(View.GONE);
                toolbar.setVisibility(View.VISIBLE);
                isShowBar = false;
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 9999) {
            try {
                String[] times = data.getStringExtra("times").split(";;");

                tipbar.setVisibility(View.VISIBLE);
                tvtip.setText("温馨提示: 当前查询时间为 " + times[0] + "至" + times[1] + "");

                start = times[0];
                end = times[1];
                loaddata();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
